package com.dgy.project.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.dgy.common.constants.ProjectConstants;
import com.dgy.project.domain.ProjectApply;
import com.dgy.project.mapper.ProjectApplyMapper;
import com.dgy.project.pojo.dto.AssignExpertDTO;
import com.dgy.project.pojo.dto.ProjectAuditListDTO;
import com.dgy.project.pojo.vo.ProjectAuditListVO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgy.project.mapper.ProjectApplyAuditMapper;
import com.dgy.project.domain.ProjectApplyAudit;
import com.dgy.project.service.IProjectApplyAuditService;

/**
 * 项目专家评审Service业务层处理
 *
 * @author ccc
 * @date 2025-04-01
 */
@Service
public class ProjectApplyAuditServiceImpl implements IProjectApplyAuditService
{
    @Autowired
    private ProjectApplyAuditMapper projectApplyAuditMapper;

    @Autowired
    private ProjectApplyMapper projectApplyMapper;

    /**
     * 查询项目专家评审
     *
     * @param reviewId 项目专家评审主键
     * @return 项目专家评审
     */
    @Override
    public ProjectApplyAudit selectProjectApplyAuditByReviewId(Long reviewId)
    {
        return projectApplyAuditMapper.selectProjectApplyAuditByReviewId(reviewId);
    }

    /**
     * 查询项目专家评审列表
     *
     * @param projectApplyAudit 项目专家评审
     * @return 项目专家评审
     */
    @Override
    public List<ProjectApplyAudit> selectProjectApplyAuditList(ProjectApplyAudit projectApplyAudit)
    {
        return projectApplyAuditMapper.selectProjectApplyAuditList(projectApplyAudit);
    }

    /**
     * 新增项目专家评审
     *
     * @param projectApplyAudit 项目专家评审
     * @return 结果
     */
    @Override
    public int insertProjectApplyAudit(ProjectApplyAudit projectApplyAudit)
    {
        projectApplyAudit.setCreateTime(DateUtils.getNowDate());
        return projectApplyAuditMapper.insertProjectApplyAudit(projectApplyAudit);
    }

    @Override
    public int assignExpert(AssignExpertDTO assignExpertDTO) {
        // 查询申报详情
        ProjectApply projectApply = projectApplyMapper.selectProjectApplyById(assignExpertDTO.getApplyId());
        if(ObjectUtil.isEmpty(projectApply) || ProjectConstants.FLAG_YES.equals(projectApply.getDelFlag())) {
            throw new RuntimeException("不存在该申报记录");
        }
        if(!ProjectConstants.AUDIT_STATUS_SUBMIT.equals(projectApply.getApplyStatus())) {
            throw new RuntimeException("当前申报记录状态不允许指派专家");
        }
        // 查询已经指派的专家
        ProjectApplyAudit queryApplyAudit = new ProjectApplyAudit();
        queryApplyAudit.setApplyId(assignExpertDTO.getApplyId());
        List<ProjectApplyAudit> oldProjectApplyAudits = projectApplyAuditMapper.selectProjectApplyAuditList(queryApplyAudit);
        // 过滤掉已经分配的专家
        List<ProjectApplyAudit> insertProjectApplyAudits = assignExpertDTO.getExpertUserList();
        insertProjectApplyAudits.forEach(audit -> {
            audit.setApplyId(projectApply.getApplyId());
            audit.setProjectId(projectApply.getProjectId());
        });
        if (CollUtil.isNotEmpty(oldProjectApplyAudits)) {
            // 获取已分配专家的 ID 列表
            List<Long> assignedUserIds = oldProjectApplyAudits.stream()
                    .map(ProjectApplyAudit::getExpertUserId)
                    .collect(Collectors.toList());
            Iterator<ProjectApplyAudit> iterator = insertProjectApplyAudits.iterator();
            while (iterator.hasNext()) {
                ProjectApplyAudit element = iterator.next();
                // 删除已经分派的专家
                if (assignedUserIds.contains(element.getExpertUserId())) {
                    iterator.remove();
                }
            }
        }
        // 新增指派专家
        if(CollUtil.isNotEmpty(insertProjectApplyAudits)) {
            projectApplyAuditMapper.insertProjectApplyAuditBatch(insertProjectApplyAudits);
        }
        return 1;
    }

    /**
     * 修改项目专家评审
     *
     * @param projectApplyAudit 项目专家评审
     * @return 结果
     */
    @Override
    public int updateProjectApplyAudit(ProjectApplyAudit projectApplyAudit)
    {
        projectApplyAudit.setUpdateTime(DateUtils.getNowDate());
        return projectApplyAuditMapper.updateProjectApplyAudit(projectApplyAudit);
    }

    /**
     * 批量删除项目专家评审
     *
     * @param reviewIds 需要删除的项目专家评审主键
     * @return 结果
     */
    @Override
    public int deleteProjectApplyAuditByReviewIds(Long[] reviewIds)
    {
        return projectApplyAuditMapper.deleteProjectApplyAuditByReviewIds(reviewIds);
    }

    /**
     * 删除项目专家评审信息
     *
     * @param reviewId 项目专家评审主键
     * @return 结果
     */
    @Override
    public int deleteProjectApplyAuditByReviewId(Long reviewId)
    {
        return projectApplyAuditMapper.deleteProjectApplyAuditByReviewId(reviewId);
    }

    @Override
    public List<ProjectAuditListVO> selectMyAuditList(ProjectAuditListDTO projectAuditListDTO) {
        return projectApplyAuditMapper.selectMyAuditList(projectAuditListDTO);
    }
}
